package com.arykow.engine.onwebapp.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class FeedsContainer extends Composite {
	private ApplicationEntryPoint applicationEntryPoint;

	private static FeedsContainerUiBinder uiBinder = GWT.create(FeedsContainerUiBinder.class);

	interface FeedsContainerUiBinder extends UiBinder<Widget, FeedsContainer> {
	}

	public FeedsContainer(ApplicationEntryPoint applicationEntryPoint) {
		initWidget(uiBinder.createAndBindUi(this));
		this.applicationEntryPoint = applicationEntryPoint;
		importButton.setText("Importer");
		addButton.setText("S'abonner");
		dialogBox.center();
		dialogBox.hide();
		dialogBoxImport.center();
		dialogBoxImport.hide();
		dialogBoxButtonSend.setText("Ajouter");
		dialogBoxButtonClose.setText("Annuler");
		dialogBoxButtonImportExecute.setText("Importer");
		dialogBoxButtonImportAbort.setText("Annuler");
		dialogBoxTextBoxUrl.setValue("http://www.10minutesaperdre.fr/feed/rss/");
		dialogBoxImportForm.setEncoding(FormPanel.ENCODING_MULTIPART);
	}

	@UiField
	FileUpload dialogBoxImportFormFile;
	
	@UiField
	Button addButton;

	@UiField
	Button importButton;

	@UiField
	Panel feedsPanel;

	@UiField
	DialogBox dialogBox;

	@UiField
	TextBox dialogBoxTextBoxUrl;

	@UiField
	Button dialogBoxButtonSend;

	@UiField
	Button dialogBoxButtonClose;

	@UiField
	DialogBox dialogBoxImport;

	@UiField
	FormPanel dialogBoxImportForm;

	@UiField
	Button dialogBoxButtonImportAbort;

	@UiField
	Button dialogBoxButtonImportExecute;

	@UiHandler("dialogBoxImportForm")
	public void onSubmitComplete(SubmitCompleteEvent event) {
		applicationEntryPoint.updateFeedsContainer();
	}

	@UiHandler("dialogBoxImportForm")
	public void onSubmit(SubmitEvent event) {
	}

	@UiHandler("dialogBoxButtonClose")
	void onClickCloseButton(ClickEvent e) {
		sendUrl(false);
	}

	@UiHandler("dialogBoxButtonImportAbort")
	void onClickImportAbort(ClickEvent e) {
		sendFile(false);
	}

	@UiHandler("dialogBoxButtonImportExecute")
	void onClickImportExecute(ClickEvent e) {
		sendFile(true);
	}

	@UiHandler("dialogBoxButtonSend")
	void onClickSendButton(ClickEvent e) {
		sendUrl(true);
	}

	@UiHandler("addButton")
	void onClickAddButton(ClickEvent e) {
		addButton.setEnabled(false);
		dialogBox.center();
		dialogBoxTextBoxUrl.setFocus(true);
		dialogBoxTextBoxUrl.setText("");
	}

	@UiHandler("importButton")
	void onClickImportButton(ClickEvent e) {
		importButton.setEnabled(false);
		dialogBoxImport.center();
	}

	@UiHandler("dialogBoxTextBoxUrl")
	public void onKeyDown(KeyDownEvent event) {
		switch (event.getNativeKeyCode()) {
		case KeyCodes.KEY_ENTER:
			sendUrl(true);
			break;
		default:
			break;
		}
	}

	private void sendUrl(boolean b) {
		if (b) {
			applicationEntryPoint.addFeed(dialogBoxTextBoxUrl.getText());
		}
		dialogBox.hide();
		addButton.setEnabled(true);
		addButton.setFocus(true);
	}

	private void sendFile(boolean b) {
		if (b) {
			dialogBoxImportForm.submit();
			// applicationEntryPoint.addFeed(dialogBoxTextBoxUrl.getText());
		}
		dialogBoxImport.hide();
		importButton.setEnabled(true);
		importButton.setFocus(true);
	}

	public void updateFeeds(List<Feed> feeds) {
		feedsPanel.clear();
		for (Feed feed : feeds) {
			feedsPanel.add(new FeedsContainerElement(feed));
		}
	}

}
